package com.migu.spms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.migu.spms.constant.Constant;
import com.migu.spms.dao.entity.ChangeHistoryCriteria;
import com.migu.spms.dao.entity.FunctionRequireCriteria;
import com.migu.spms.dao.entity.FunctionRequireDO;
import com.migu.spms.dao.inf.IChangeHistoryDO;
import com.migu.spms.dao.inf.IFunctionRequireDO;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IOnlineVersionService;
import com.migu.spms.utils.CodeCreateUtil;
import com.migu.spms.utils.exception.ServiceException;
import com.migu.spms.webapi.data.UpdateOnlineVersionRequest;

@Component("onlineVersionService")
public class OnlineVersionServiceImpl implements IOnlineVersionService {
    private static Logger logger = LoggerFactory.getLogger(OnlineVersionServiceImpl.class);
    @Resource(name = "IFunctionRequireDO")
    private IFunctionRequireDO functionRequireDAO;
    @Resource(name = "IChangeHistoryDO")
    private IChangeHistoryDO changeHistoryDAO;

    @Override
    public void update(UpdateOnlineVersionRequest updateOnlineVersionRequest, User user) throws Exception {
        List<Long> ids = updateOnlineVersionRequest.getFunctionRequireIds();
        if (null == ids || ids.size() < 1) {
            logger.error(CodeCreateUtil.createLogMessage("OnlineVersionServiceImpl.update", "更新上线版本[需求主键为空]"));
            throw new ServiceException("需求主键不能为空！");
        }
        for (Long id : ids) {
            FunctionRequireDO functionRequireDO = functionRequireDAO.selectByPrimaryKey(id);
            if (functionRequireDO == null) {
                logger.error(CodeCreateUtil.createLogMessage("OnlineVersionServiceImpl.update",
                        "更新上线版本[根据需求id：" + id + "查询需求为空]"));
                throw new ServiceException("更新失败，请重试");
            }
            FunctionRequireCriteria functionRequireCriteria = new FunctionRequireCriteria();
            functionRequireCriteria.setId(id);
            functionRequireCriteria.setPlanOnlineVersion(updateOnlineVersionRequest.getVersion());
            functionRequireDAO.updateByPrimaryKeySelective(functionRequireCriteria);
            insertOperateHistory(id, user, functionRequireDO.getPlanOnlineVersion(),
                    updateOnlineVersionRequest.getVersion());
        }
    }

    /**
     * @Description 插入操作历史 
     * @param functionRequireDO
     * @param user
     */
    private void insertOperateHistory(Long functionRequireId, User user, String oldVersion, String newVersion) {
        ChangeHistoryCriteria changeHistoryCriteria = new ChangeHistoryCriteria();
        // changeHistoryCriteria.setBaselineVersion(functionRequireCriteria.getBaselineVersion());
        changeHistoryCriteria.setFunctionRequireId(functionRequireId);
        changeHistoryCriteria.setCreateId(Long.valueOf(user.getUserId()));
        changeHistoryCriteria.setCreateName(user.getUserName());
        changeHistoryCriteria.setOperateType(Constant.OperateType.UPDATE_ONLINE_VERSION);
        changeHistoryCriteria.setCreateTime(new Date());
        changeHistoryCriteria.setIsPermit(Constant.LogicVariable.NO_INT.toString());
        changeHistoryCriteria.setIsDetails(Constant.LogicVariable.NO_INT.toString());
        changeHistoryCriteria.setBeforePermit("===修改前：" + oldVersion);
        changeHistoryCriteria.setAfterPermit("===修改后: " + newVersion);
        changeHistoryCriteria.setRemark(CodeCreateUtil.updateOnlineVersionRemark(oldVersion, newVersion));
        changeHistoryDAO.insertSelective(changeHistoryCriteria);
    }
}
